﻿using NCP.Model;
using SqlSugar;
using System.Collections.Generic;

namespace NCP.Service
{

    public class BaseService<T> where T : BaseModel, new()
    {
        protected SqlSugarClient _client;

        public virtual bool Add(T model)
        {
            return _client.Insertable<T>(model).ExecuteCommand() > 0;
        }
        public virtual int AddReturn(T model)
        {
            return _client.Insertable<T>(model).ExecuteReturnIdentity();
        }
        public virtual int AddList(List<T> modelList)
        {
            return _client.Insertable<T>(modelList).ExecuteCommand();
        }

        public virtual int Update(T model)
        {
            return _client.Updateable<T>(model).ExecuteCommand();
        }

        public virtual int Delete(int Id)
        {
            return _client.Updateable<T>(new Dictionary<string, object>() { { "Status", "-1" } })
                .Where(t => t.Id.Equals(Id)).ExecuteCommand();
        }
        //物理删除
        public virtual int DelById(int Id)
        {
            return _client.Deleteable<T>().Where(t => t.Id == Id).ExecuteCommand();
        }

        public virtual T Get(int Id)
        {
            return _client.Queryable<T>().First(t => t.Id == Id);
        }

        public virtual int Save(T model)
        {
            if (model.Id > 0)
            {
                return Update(model);
            }
            else
            {
                return Add(model) ? 1 : 0;
            }
        }
        //改为逻辑删除
        public virtual bool DelByIds(string Ids)
        {
            Ids = Ids.Trim(',');
            string[] IdArry = Ids.Split(',');
            int rst = 0;
            rst = _client.Updateable<T>(new Dictionary<string, object>() { { "Status", "-1" } })
                .Where(t => SqlFunc.ContainsArray<string>(IdArry, t.Id.ToString()))
                .ExecuteCommand();
            return rst > 0;
        }

        //正式删除
        public virtual bool DelByIds1(string Ids)
        {
            Ids = Ids.Trim(',');
            string[] IdArry = Ids.Split(',');
            int rst = 0;
            rst = _client.Deleteable<T>()
               .Where(t => SqlFunc.ContainsArray<string>(IdArry, t.Id.ToString()))
               .ExecuteCommand();
            return rst > 0;
        }

        public virtual List<T> GetAll(Expressionable<T> exp, string orderFields = "Id")
        {
            return _client.Queryable<T>().Where(exp.ToExpression()).OrderBy(orderFields).ToList();
        }

        public virtual List<T> GetAll(Expressionable<T> exp, int num, string orderFields = "Id")
        {
            return _client.Queryable<T>().Where(exp.ToExpression()).OrderBy(orderFields).Take(num).ToList();
        }

        public virtual List<T> GetAll()
        {
            return _client.Queryable<T>().ToList();
        }

        public virtual List<T> GetPageList(Expressionable<T> whereExpression, PageModel page, string orderFields = "Id")
        {
            int count = 0;
            var result = _client.Queryable<T>().Where(whereExpression.ToExpression()).OrderBy(orderFields).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.TotalCount = count;
            return result;
        }
    }
}
